/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */
// @ts-ignore
class TreeNode {
  val: number
  left: TreeNode | null
  right: TreeNode | null

  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = (val === undefined ? 0 : val)
    this.left = (left === undefined ? null : left)
    this.right = (right === undefined ? null : right)
  }
}

// @ts-ignore
function sumNumbers(root: TreeNode | null): number {
  let sum = 0
  const bst = (current: number, node: TreeNode) => {
    if (!node) return;
    if (!node.left && !node.right) {
      sum += current * 10 + node.val;
      return;
    }
    bst(current + node.val,node.left!)
    bst(current + node.val,node.right!)
  }
  bst(0, root!)
  return sum
};
